package com.aidong.web.controller.card;

/**
 * Created by john on 16-11-3.
 */
import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.base.OrderSet;
import com.aidong.dao.base.Pagination;
import com.aidong.dao.wills.GiftCardDao;
import com.aidong.model.wills.GiftCard;
import com.aidong.web.controller.BaseController;
import java.util.Date;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({"giftCardLoss"})
public class GiftCardLossController extends BaseController {
    @Resource
    GiftCardDao giftCardDao;

    public GiftCardLossController() {
    }

    @RequestMapping({"list"})
    public ModelAndView list() {
        return (new ModelAndView("giftcard/loss/list")).addObject("shopName", this.getTypeList("shopName")).addObject("cardType", this.getTypeList("giftCardType")).addObject("cardStatus", this.getTypeList("cardStatus"));
    }

    @RequestMapping({"_list"})
    public ModelAndView _list(@RequestParam(value = "shopCode",required = false) String shopCode, @RequestParam(value = "cardType",required = false) String cardType, @RequestParam(value = "status",required = false) Integer status, @RequestParam(value = "noFrom",required = false) String noFrom, @RequestParam(value = "noTo",required = false) String noTo, @RequestParam(value = "pageNo",required = false) String pageNo, @RequestParam(value = "totalCount",required = false) String totalCount) {
        Pagination pg = new Pagination(1, 20);
        pg.setPage(this.parseInt(pageNo), this.parseInt(totalCount));
        ConditionMap cd = new ConditionMap();
        this.addConditionShopCode(cd, "shopCode", shopCode);
        cd.eq("cardType", cardType);
        cd.eq("status", status);
        cd.ge("cardCode", noFrom);
        cd.le("cardCode", noTo);
        cd.eq("reportLoss", Integer.valueOf(1));
        this.giftCardDao.queryPageList(pg, cd, (new OrderSet()).asc("cardCode"));
        ModelAndView mv = new ModelAndView("giftcard/loss/_list");
        mv.addObject("list", pg.getResultList());
        mv.addObject("pg", pg);
        return mv;
    }

    @RequestMapping({"add"})
    public ModelAndView add() {
        ModelAndView mv = new ModelAndView("giftcard/loss/add");
        mv.addObject("_oper", "add");
        return mv;
    }

    @RequestMapping({"_add"})
    @ResponseBody
    public String _add(@RequestParam("code") String code) {
        GiftCard card = this.giftCardDao.getByCardCode(code);
        if(card == null) {
            return this.getResult(false, "没有这个储值卡");
        } else if(card.getReportLoss().intValue() == 1) {
            return this.getResult(false, "储值卡已经挂失");
        } else {
            try {
                card.setReportLoss(Integer.valueOf(1));
                card.setLossUser(this.getLoginUserCode());
                card.setLossDate(new Date());
                this.giftCardDao.updateLoss(card);
                this.addUserLog("储值卡挂失", "卡号:" + card.getCardCode());
            } catch (Exception var4) {
                this.log.error("add giftcard loss", var4);
                return this.getResult(false, "保存发生错误");
            }

            return this.getResult(true, "挂失成功");
        }
    }

    @RequestMapping({"_del"})
    @ResponseBody
    public String _del(@RequestParam("code") String code) {
        GiftCard card = this.giftCardDao.getByCardCode(code);
        if(card == null) {
            return this.getResult(false, "没有这个储值卡");
        } else if(card.getReportLoss().intValue() == 0) {
            return this.getResult(false, "储值卡没有挂失");
        } else {
            try {
                this.giftCardDao.cancelLoss(card.getCardCode());
                this.addUserLog("储值卡取消挂失", "卡号:" + card.getCardCode());
            } catch (Exception var4) {
                this.log.error("del giftcard loss", var4);
                return this.getResult(false, "保存发生错误");
            }

            return this.getResult(true, "取消挂失成功");
        }
    }
}
